<template>
  <el-dialog :title="title" :visible="showDialog" @close="cancelBtn">
    <el-form ref="formRef" :rules="rules" :model="formData" label-width="80px">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="角色描述" prop="description">
        <el-input v-model="formData.description" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelBtn">取 消</el-button>
      <el-button type="primary" @click="OkBtn">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addRole, getRoledetail, editRole } from '@/api/setting.js'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      title: '新增角色',
      formData: {},
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 确定
    OkBtn() {
      // 手动验证
      this.$refs.formRef.validate(async isok => {
        if (isok) {
          if (this.formData.id) {
            // 编辑
            await editRole(this.formData)
          } else {
            // 新增
            await addRole(this.formData)
          }
          this.$message.success('操作成功!')
          this.$emit('getRoleList')
          this.$emit('update:showDialog', false)
          this.formData = {}
          this.$refs.formRef.resetFields()
        }
      })
    },
    cancelBtn() {
      this.formData = {}
      this.$refs.formRef.resetFields()
      this.$emit('update:showDialog', false)
    },
    // 获取详情
    async getRoledetail(id) {
      this.title = '编辑角色'
      const ret = await getRoledetail(id)
      this.formData = ret
    }
  }
}
</script>

<style scoped>
.el-form{
  padding: 0 10px 0 50px;
}

</style>
